﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ZDSoft.TES.Entity;


namespace ZDSoft.TES.Web.Helpers
{
    public delegate string InvokeMethod();

    public static class JsonHelper
    {
        //获得只有简单类型数据JSON字符窜
        public static string ToJsonString<T>(this T t, IList<KeyValuePair<string, InvokeMethod>> invokeMethods)
            where T : EntityBase<T>
        {
            IList<string> systemPropertyType = new List<string> { "System.String", "System.Boolean", "System.Int32" };
            string tmpResult = "{";
            foreach (PropertyInfo property in t.GetType().GetProperties())
            {
                IList<bool> monitor = new List<bool>();
                foreach (string sysProperty in systemPropertyType)
                {
                    if (property.PropertyType.ToString() != sysProperty)
                    {
                        monitor.Add(false);
                    }
                    else
                    {
                        monitor.Add(true);
                    }
                }
                int tmp = 0;
                foreach (bool i in monitor)
                {
                    if (i == false)
                    {
                        tmp++;
                    }
                }
                if (tmp != monitor.Count)
                {
                    tmpResult += "\"" + property.Name + "\":\"" + property.GetValue(t, null) + "\",";
                }
                else
                {
                    tmpResult += "\"" + property.Name + "\":\"\",";
                }
            }
            if (invokeMethods != null)
            {
                foreach (KeyValuePair<string, InvokeMethod> kvp in invokeMethods)
                {
                    tmpResult += "\"" + kvp.Key + "\":";
                    if (kvp.Value != null)
                    {
                        tmpResult += "\"" + kvp.Value() + "\",";
                    }
                    else
                    {
                        tmpResult += "\"\",";
                    }
                }
            }
            tmpResult = tmpResult.Substring(0, tmpResult.Length - 1);
            tmpResult += "}";
            return tmpResult;
        }

        public static T ToJaonResult<T>(this T t)
            where T : EntityBase<T>, new()
        {
            T dto = new T();
            IList<string> systemPropertyType = new List<string> { "System.String", "System.Boolean", "System.Int32" };
            foreach (PropertyInfo property in t.GetType().GetProperties())
            {
                if (systemPropertyType.Contains(property.PropertyType.ToString()))
                {
                    object val = property.GetValue(t, null);
                    property.SetValue(dto, val, null);
                }
            }
            return dto;
        }
    }
}
